<template>
  <div class="app-container">
    <!--项目基本信息-->
    <el-row>
      <el-col :span="24" class="card-box">
        <el-card>
          <div slot="header"><span><i class="el-icon-monitor"></i> 基本信息</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <table cellspacing="0" style="width: 100%">
              <tbody>
              <tr>
                <td class="el-table__cell is-leaf is-title">
                  <div class="cell">缴费项目</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="activity">{{ activity.actName }}</div>
                </td>
                <td class="el-table__cell is-leaf is-title">
                  <div class="cell">缴费金额</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="activity">{{ activity.totalPrice }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf is-title">
                  <div class="cell">缴费说明</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="activity">{{ activity.remark }}</div>
                </td>
                <td class="el-table__cell is-leaf is-title">
                  <div class="cell">所属学期</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="activity">{{ activity.semester ==='Q'? '秋季学期':'春季学期' }}</div>
                </td>
              </tr>
              <tr>
                <td class="el-table__cell is-leaf is-title">
                  <div class="cell">所属班级</div>
                </td>
                <td class="el-table__cell is-leaf">
                  <div class="cell" v-if="dept">{{ dept.deptName }}</div>
                </td>
                <td class="el-table__cell is-leaf is-title">
                  <div class="cell"></div>
                </td>
                <td class="el-table__cell is-leaf">
<!--                  <div class="cell" v-if="activity"></div>-->
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-card>
        <!--部门数据-->
        <el-col :span="4" :xs="24" v-if="false">
          <div class="head-container">
            <el-input
              v-model="deptName"
              placeholder="请输入节点名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="deptOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="id"
              default-expand-all
              highlight-current
              @node-click="handleNodeClick"
            />
          </div>
        </el-col>
        <!--用户数据-->
        <el-col :span="20" :xs="24">
          <!--    搜索区    -->
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                   label-width="68px">
            <el-form-item label="学号" prop="stuNo">
              <el-input
                v-model="queryParams.stuNo"
                placeholder="请输入学号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="姓名" prop="totalPrice">
              <el-input
                v-model="queryParams.studentName"
                placeholder="请输入姓名"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <!--学生列表 -->
          <el-table v-loading="loading" :data="studentsList" ref="singleTable" @selection-change="handleSelectionChange"
                    :row-key="row => row.studentId">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="学号" align="center" prop="stuNo"/>
            <el-table-column label="姓名" align="center" prop="studentName"/>
            <el-table-column label="班级" align="center" key="deptName" prop="school.deptName"

            />
            <el-table-column label="手机号码" align="center" prop="phonenumber"
            />
            <el-table-column label="用户性别" align="center" prop="sex">
              <template slot-scope="scope">{{ scope.row.sex == 1 ? '女' : '男' }}</template>
            </el-table-column>
            <el-table-column label="状态" align="center" key="status">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  active-value="0"
                  inactive-value="1"
                  :disabled="true"
                ></el-switch>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['campus:students:edit']"
                >查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!--    分页组件        -->
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-card>
    </el-row>
    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <div slot="header"><span><i class="el-icon-monitor"></i> 基本信息</span></div>
      <div class="el-table el-table--enable-row-hover el-table--medium">
        <table cellspacing="0" style="width: 100%">
          <tbody>
          <tr>
            <td class="el-table__cell is-leaf is-title">
              <div class="cell">学号</div>
            </td>
            <td class="el-table__cell is-leaf">
              <div class="cell" v-if="student">{{ student.stuNo }}</div>
            </td>
            <td class="el-table__cell is-leaf is-title">
              <div class="cell">姓名</div>
            </td>
            <td class="el-table__cell is-leaf">
              <div class="cell" v-if="student">{{ student.studentName }}</div>
            </td>
          </tr>
          <tr>
            <td class="el-table__cell is-leaf is-title">
              <div class="cell">班级</div>
            </td>
            <td class="el-table__cell is-leaf">
              <div class="cell" v-if="dept">{{ dept.deptName }}</div>
            </td>
            <td class="el-table__cell is-leaf is-title">
              <div class="cell">手机号码</div>
            </td>
            <td class="el-table__cell is-leaf">
              <div class="cell" v-if="student">{{ student.phonenumber }}</div>
            </td>
          </tr>
          </tbody>
        </table>
      </div>
    </el-dialog>

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import {getStudents, listStudents} from "@/api/campus/students";
import {deptSchoolTreeSelect} from "@/api/campus/Org";
import {getActivity} from "@/api/campus/activity";
import {addActStudents, getActStudents} from "@/api/campus/act_stu";

export default {
  name: "add-stu",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 10,
      // 用户信息
      form: {},
      //活动信息
      activity: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 部门名称
      deptName: undefined,
      dept: {},
      // 学生表格数据
      studentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        stuNo: null,
        studentName: null,
        userName: undefined,
        phonenumber: undefined,
        deptId: undefined,
        sex: null,
        status: null,
      },
      // 回显学生信息
      student: {},

    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    const actId = this.$route.params && this.$route.params.actId;
    if (actId) {
      this.loading = true;
      getActStudents(actId).then((response) => {
        this.activity = response.data.activity;
        this.studentsList = response.data.deptStudents;
        this.ids = response.data.students;
        this.dept = response.data.activity.dept;
        this.toggleSelection(response.data.students);
        this.loading = false;
      });
    }

    // this.getDeptTree();
  },

  methods: {
    /** 提交按钮 */
    submitForm() {
      const actId = this.activity.actId;
      const students = this.ids.join(",");
      const data = {
        actId: actId,
        students: students
      }
      addActStudents(data).then((response) => {
        this.$modal.msgSuccess("授权成功");
        this.close();
      });
    },
    /** 关闭按钮 */
    close() {
      const obj = {path: "/activity/activity"};
      this.$tab.closeOpenPage(obj);
    },
    /***********************************************学生查询************************************************** */
    /** 查询学生列表 */
    getList() {
      this.loading = true;
      listStudents(this.queryParams).then(response => {
        this.studentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 回显选中的学生 有问题 TODO */
    setCurrent(row) {
      this.$refs.singleTable.setCurrentRow(row);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.singleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.singleTable.clearSelection();
      }
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptSchoolTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.studentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看操作 */
    handleUpdate(row) {
      const studentId = row.studentId || this.ids
      getStudents(studentId).then(response => {
        this.student = response.data;
        this.open = true;
        this.title = "修改学生";
      });
    },
  },
};
</script>

<style>
.is-title {
  background-color: #f0f0f0; /* 设置背景色为灰色，您可以根据需求更改颜色值 */
  width: 100px;
}

.is-leaf {
  width: 60px;
}
</style>
